﻿#region imports

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;

using CondeNet.AssetManager.Data;
using CondeNet.AssetManager.Business;
using CondeNet.AssetManager.Business.Interface;

#endregion

namespace CondeNet.AssetManager.DataProvider.Sql.Mappers
{
    public static class AssetContentMapper
    {
        internal static IAssetContent ToBusinessObject ( T_AssetContent entity )
        {
            IAssetContent obj = new AssetContent (
                entity.AssetContent_IDPK ,
                entity.AssetID_FK.Value ,
                entity.AssetPath_NVC ,
                entity.AssetContentTypeID_FK ,
                entity.MimeTypeID_FK ,
                entity.StatusID_FK ,
                entity.LanguageID_FK ,
                entity.CreatorID_NVC ,
                entity.LatestEditorID_NVC ,
                entity.CreationDate_DT ,
                entity.UpdatedDate_DT );

            // parent is causing recursion: obj.Asset = AssetMapper.ToBusinessObject ( entity.T_Asset );
            obj.AssetContentType = AssetContentTypeMapper.ToBusinessObject ( entity.T_AssetContentType );
            obj.Status = StatusMapper.ToBusinessObject ( entity.T_Status );
            obj.Language = LanguageMapper.ToBusinessObject ( entity.T_Language );
            obj.MimeType = MimeTypeMapper.ToBusinessObject ( entity.T_MimeType );

            obj.AssetContentMetaDataList = AssetContentMetaDataMapper.ToBusinessObjects ( entity.T_AssetContentMetaDatas );
            obj.AssetContentRepositoryList = AssetContentRepositoryMapper.ToBusinessObjects ( entity.T_AssetContentRepositories );

            return obj;
        }

        internal static T_AssetContent ToEntity ( IAssetContent v )
        {
            T_AssetContent entity = new T_AssetContent ( );

            //entity.AssetContent_IDPK = v.ID;
            entity.AssetID_FK = v.AssetID;
            entity.AssetPath_NVC = v.AssetPath;
            entity.AssetContentTypeID_FK = v.AssetContentTypeID;
            entity.MimeTypeID_FK = v.MimeTypeID;
            entity.StatusID_FK = v.StatusID;
            entity.LanguageID_FK = v.LanguageID;
            entity.CreatorID_NVC = v.CreatorID;
            entity.LatestEditorID_NVC = v.LatestEditorID;
            entity.CreationDate_DT = v.CreationDate;
            entity.UpdatedDate_DT = v.UpdatedDate;

            // parent is causing recursion: entity.T_Asset = AssetMapper.ToEntity ( v.Asset );
            if ( v.AssetContentType != null ) entity.T_AssetContentType = AssetContentTypeMapper.ToEntity ( v.AssetContentType );
            if ( v.Status != null ) entity.T_Status = StatusMapper.ToEntity ( v.Status );
            if ( v.Language != null ) entity.T_Language = LanguageMapper.ToEntity ( v.Language );
            if ( v.MimeType != null ) entity.T_MimeType = MimeTypeMapper.ToEntity ( v.MimeType );

            if ( v.AssetContentMetaDataList != null ) entity.T_AssetContentMetaDatas = ( System.Data.Linq.EntitySet<T_AssetContentMetaData> ) AssetContentMetaDataMapper.ToEntities (v.ID, v.AssetContentMetaDataList );

            if (v.AssetContentRepositoryList != null) entity.T_AssetContentRepositories = (System.Data.Linq.EntitySet<T_AssetContentRepository>)AssetContentRepositoryMapper.ToEntities(v.ID, v.AssetContentRepositoryList);

            return entity;
        }

        internal static IList<IAssetContent> ToBusinessObjects ( IList<T_AssetContent> dalList )
        {
            return dalList.Select ( tmp => ToBusinessObject ( tmp ) ).ToList ( );
        }

        internal static IList<T_AssetContent> ToEntities ( IList<IAssetContent> entityList )
        {
            EntitySet<T_AssetContent> list = new EntitySet<T_AssetContent>();
            entityList.Select ( tmp => ToEntity ( tmp ) ).ToList ( ).ForEach(list.Add);
            return list;
        }
    }
}
